package cn.springstudio.springui.base;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.ybq.android.spinkit.SpinKitView;
import com.github.ybq.android.spinkit.SpriteFactory;
import com.github.ybq.android.spinkit.Style;
import com.github.ybq.android.spinkit.sprite.Sprite;
import com.wang.avi.AVLoadingIndicatorView;

import cn.springstudio.springui.R;
import cn.springstudio.springui.databinding.ActivityProgressBarBinding;

public class ProgressBarActivity extends AppCompatActivity {

    private static final int AVLoadingIndicator = 1;
    private static final int SpinKit = 2;
    ActivityProgressBarBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityProgressBarBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.recycler.setAdapter(new RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                if (viewType == SpinKit) {
                    View itemView = getLayoutInflater().inflate(R.layout.item_spinkit, parent, false);
                    return new SpinKitHolder(itemView);
                } else {
                    View itemView = getLayoutInflater().inflate(R.layout.item_av_loading_indicator, parent, false);
                    return new IndicatorHolder(itemView);
                }
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
                if (holder instanceof IndicatorHolder) {
                    String style = INDICATORS[position % INDICATORS.length];
                    ((IndicatorHolder)holder).indicatorView.setIndicator(style);
                    ((IndicatorHolder)holder).nameView.setText("AVLoadingIndicatorView\n" + style);
                } else if (holder instanceof SpinKitHolder) {
                    Style style = Style.values()[position - INDICATORS.length];
                    Sprite drawable = SpriteFactory.create(style);
                    ((SpinKitHolder)holder).indicatorView.setIndeterminateDrawable(drawable);
                    ((SpinKitHolder)holder).nameView.setText("SpinKitView\n" + style.name());
                }
            }

            @Override
            public int getItemCount() {
                return INDICATORS.length + Style.values().length;
            }

            @Override
            public int getItemViewType(int position) {
                if (position < INDICATORS.length) {
                    return AVLoadingIndicator;
                } else if (position < INDICATORS.length + Style.values().length) {
                    return SpinKit;
                }
                return super.getItemViewType(position);
            }
        });
    }

    final static class IndicatorHolder extends RecyclerView.ViewHolder{

        public AVLoadingIndicatorView indicatorView;
        private final TextView nameView;

        public IndicatorHolder(View itemView) {
            super(itemView);
            indicatorView = itemView.findViewById(R.id.indicator);
            nameView = itemView.findViewById(R.id.name);
        }
    }

    final static class SpinKitHolder extends RecyclerView.ViewHolder{

        public SpinKitView indicatorView;
        private final TextView nameView;

        public SpinKitHolder(View itemView) {
            super(itemView);
            indicatorView = itemView.findViewById(R.id.indicator);
            nameView = itemView.findViewById(R.id.name);
        }
    }

    private static final String[] INDICATORS=new String[]{
            "BallPulseIndicator",
            "BallGridPulseIndicator",
            "BallClipRotateIndicator",
            "BallClipRotatePulseIndicator",
            "SquareSpinIndicator",
            "BallClipRotateMultipleIndicator",
            "BallPulseRiseIndicator",
            "BallRotateIndicator",
            "CubeTransitionIndicator",
            "BallZigZagIndicator",
            "BallZigZagDeflectIndicator",
            "BallTrianglePathIndicator",
            "BallScaleIndicator",
            "LineScaleIndicator",
            "LineScalePartyIndicator",
            "BallScaleMultipleIndicator",
            "BallPulseSyncIndicator",
            "BallBeatIndicator",
            "LineScalePulseOutIndicator",
            "LineScalePulseOutRapidIndicator",
            "BallScaleRippleIndicator",
            "BallScaleRippleMultipleIndicator",
            "BallSpinFadeLoaderIndicator",
            "LineSpinFadeLoaderIndicator",
            "TriangleSkewSpinIndicator",
            "PacmanIndicator",
            "BallGridBeatIndicator",
            "SemiCircleSpinIndicator"
    };

}